package com.maotai.app.thread;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import org.apache.http.client.config.RequestConfig;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.maotai.app.bean.MaotaiAppCookie;
import com.maotai.app.service.MaotaiAppService;
import com.maotai.app.util.MaotaiAppHttpClientUtil;
import com.maotai.llzhou.util.IPFactory;

public class MaoTaiAppShopThread extends Thread {

	private MaotaiAppService maotaiAppService;
	private String ipAndPort;


	public MaoTaiAppShopThread(MaotaiAppService maotaiAppService, String ipAndPort) {
		super();
		this.maotaiAppService = maotaiAppService;
		this.ipAndPort = ipAndPort;
	}

	public String getIpAndPort() {
		return ipAndPort;
	}

	public void setIpAndPort(String ipAndPort) {
		this.ipAndPort = ipAndPort;
	}

	public MaotaiAppService getMaotaiAppService() {
		return maotaiAppService;
	}

	public void setMaotaiAppService(MaotaiAppService maotaiAppService) {
		this.maotaiAppService = maotaiAppService;
	}

	@Override
	public void run() {
		try {
			MaotaiAppCookie mwc = new MaotaiAppCookie();
			
			MaotaiAppHttpClientUtil hcu = MaotaiAppHttpClientUtil.getInstance();
			
			RequestConfig config = IPFactory.getKdRequestConfig(ipAndPort);
			int max = 150;
			int min = 50;
			Random random = new Random();
			double s1 = random.nextDouble()* (max - min + 1) + min;
			int max2 = 60;
			int min2 = 10;
			Random random2 = new Random();
			double s2 = random2.nextDouble()* (max2 - min2 + 1) + min2;
//			System.out.println(s1+"---------"+s2);
			String send_url = "https://www.emaotai.cn/yundt-application-trade-core/api/v1/yundt/trade/item/nearest/shop/get?province=%e5%b9%bf%e5%b7%9e%e5%b8%82&lat="+s2+"&lng="+s1;
			mwc = hcu.sendHttpGetProxy(send_url, mwc, config, "https://www.emaotai.cn/smartsales-b2c-web-pc/login");
			if(mwc.getContent()!=null){
				JSONObject data = JSONObject.parseObject(mwc.getContent()).getJSONObject("data");
				if(data!=null){
					String id = data.getString("id");
					String latitude = data.getString("latitude");
					String longitude = data.getString("longitude");
					String provinceCode = data.getString("provinceCode");
					String countyCode = data.getString("countyCode");
					String cityCode = data.getString("cityCode");
					String name= data.getString("name");
					String detailAddress= data.getString("detailAddress");
					Map<String,String> map = new HashMap<String,String>();
					map.put("id", id);
					map.put("latitude", latitude);
					map.put("longitude", longitude);
					map.put("provinceCode", provinceCode);
					map.put("countyCode", countyCode);
					map.put("cityCode", cityCode);
					map.put("name", name);
					map.put("detailAddress", detailAddress);
					//根据ip去看看是不是在卖飞天
					
					String checkUrl = "https://www.emaotai.cn/yundt-application-trade-core/api/v1/yundt/trade/shop/item/list?shopId="+id+"&orderKey=1&pageNum=1&pageSize=10";
					mwc = hcu.sendHttpGetProxy(checkUrl, mwc, config, null);
					if(mwc.getContent()!=null){
						JSONObject data2 = JSONObject.parseObject(mwc.getContent()).getJSONObject("data");
						if(data2!=null){
							JSONArray feitianArry = data2.getJSONArray("list");
							if(feitianArry!=null && feitianArry.size()>0){
								for(int i=0; i<feitianArry.size(); i++){
									JSONObject tempJ = feitianArry.getJSONObject(i);
									String feitianStr = tempJ.getString("name");
									String salePrice = tempJ.getString("salePrice");
									if(feitianStr.contains("新飞天")&&"1499.00".equals(salePrice)){
										maotaiAppService.insertMaotaiShop(map);
										break;
									}
								}
							}
						}
					}
					
				}
				
				
				
			}
			//接收验证码
			//TODO
		} catch (Exception e1) {
			e1.printStackTrace();
		}
	}
}
